package com.android.personal.kotlin.utils;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by zhangwentao on 2017/3/6.
 */

public class CopyAssetsFiles {

    private static final String TAG = "CopyAssetsFiles";

    //将asset中的数据库复制到本地进行读取
    public static void copyAssetsFiles(Activity context, String savePath, String dbName) {
        try {
            String outFileName = savePath + "/" + dbName;
            File file = new File(savePath);
            if (!file.mkdirs()) {
                file.mkdirs();
            }
            File dataFile = new File(outFileName);
            if (dataFile.exists()) {
                dataFile.delete();
            }
            InputStream inputStream;
            inputStream = context.getAssets().open(dbName);
            OutputStream outputStream = new FileOutputStream(outFileName);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
            Log.i(TAG, "error--->" + e.toString());
            e.printStackTrace();
        }
    }

}
